package com.huicoo.forestmanager.service;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Build;
import android.os.IBinder;

import com.huicoo.forestmanager.R;
import com.huicoo.forestmanager.base.BaseApplication;
import com.huicoo.forestmanager.util.LogUtils;
import com.huicoo.forestmanager.util.ServiceUtil;

/**
 * 后台播放音乐达到保活目的
 */
public class SingASongService extends Service {
    private MediaPlayer mMediaPlayer;
    private Thread thread;

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        MyThread myThread = new MyThread();
        thread = new Thread(myThread);
        mMediaPlayer = MediaPlayer.create(BaseApplication.getMyApplication(), R.raw.no_notice);
        mMediaPlayer.setLooping(true);

        LogUtils.d("gogogo singsong 创建播放对象：" + mMediaPlayer.hashCode());
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        try {
            if (!thread.isAlive()) {
                thread.start();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        LogUtils.d("gogogo singsong 播放时 线程名称：" + thread.getName());
        return START_STICKY;
    }

    //开始、暂停播放
    private void startPlaySong() {
        if (mMediaPlayer == null) {
            mMediaPlayer = MediaPlayer.create(BaseApplication.getMyApplication(), R.raw.no_notice);
        }
        mMediaPlayer.start();
        LogUtils.d("gogogo singsong 音乐启动播放,播放对象为： " + mMediaPlayer.hashCode());
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        mMediaPlayer.pause();
        LogUtils.d("gogogo singsong 恢复播放 时当前播放器对象：" + mMediaPlayer.hashCode());
        stopPlaySong();
        LogUtils.d("gogogo singsong 应用播放服务被杀死，正在重启");
        LogUtils.d("gogogo singsong 目标播放工作线程是否存活：" + thread.isAlive());

        //任务的前台服务不存在的话，不复活
//        boolean isForegroundServiceRunning = ServiceUtil.isServiceRunning(getApplicationContext(), "com.huicoo.forestmanager.service.ForegroundService");
//        LogUtils.d("gogogo singsong 执行任务前台服务是否在进行中 = " + isForegroundServiceRunning);
//        if (isForegroundServiceRunning) {
//            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
//                startForegroundService(new Intent(getApplicationContext(), SingASongService.class));
//            } else {
//                startService(new Intent(getApplicationContext(), SingASongService.class));
//            }
//        }
    }

    //停止播放销毁对象
    private void stopPlaySong() {
        if (mMediaPlayer != null) {
            mMediaPlayer.stop();
            LogUtils.d("gogogo singsong 音乐停止播放,播放对象为：" + mMediaPlayer.hashCode());
            LogUtils.d("gogogo singsong 音乐播放器是否在循环：" + mMediaPlayer.isLooping());
            LogUtils.d("gogogo singsong 音乐播放器是否还在播放：" + mMediaPlayer.isPlaying());
            mMediaPlayer.release();
            LogUtils.d("gogogo singsong 播放对象销毁,播放对象为：" + mMediaPlayer.hashCode());
            mMediaPlayer = null;
        }
    }

    class MyThread implements Runnable {

        @Override
        public void run() {
            startPlaySong();
        }
    }
}